package com.yq.day28;


import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityRuntimeException;

import java.util.concurrent.TimeUnit;

/**
 * @author 3307235355@qq.com
 * @since 2022/06/28 17:12
 */
public class Demo05 {
    public static final Object OBJECT = new Object();
    public static int count = 0;
    public static void main(String[] args) {

        new Thread(() -> {
            synchronized (OBJECT) {
                while (true) {
                    count++;
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    if (count == 5) {
                        System.out.println("count=" + 5);
                        // System.out.println(Thread.currentThread().getName());
                        //人为的制造异常
                        System.out.println(10/0);
                    }

                }
            }

        }, "t1").start();

        //mian sleep
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(() -> {
            System.out.println("t2 start");
            synchronized (OBJECT) {
                System.out.println("t2  访问了内容");
                // System.out.println(Thread.currentThread().getName());
            }
        }, "t2").start();
    }
}
